<%@ page import="com.xyzboom.database.entity.User" %>
<%@ page import="com.xyzboom.database.entity.HitCourse" %>
<%@ page import="java.util.List" %>
<%@ page import="com.xyzboom.database.dao.HitCourseDAO" %>
<%@ page import="com.xyzboom.web.servlets.course.ExitCourse" %>
<%@ page contentType="text/html;charset=UTF-8" language="java"
         errorPage="../error.jsp" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="xyzboom" uri="com.xyzboom.tld" %>
<%
    User user = (User) request.getSession().getAttribute("user");
//    System.out.println(user);
    if (user == null) {
        out.println("您需要登录");
        return;
    }
%>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="<xyzboom:res type="css" name="css1"/>"/>
    <title>Title</title>
</head>
<body>
<table>
    <tr style="width:250px">
        <th style="width:50%">
            学号:<%=user.getId()%>
        </th style="width:50%">
        <th>
            姓名:<%=user.getUsername()%>
        </th>
    </tr>
</table>

<table class="table_hitCourse" cellspacing="0" id="table_hitCourse">
    <tr>
        <th>&nbsp;&nbsp;&nbsp;</th>
        <th>序号</th>
        <th>课程名称</th>
        <th>分数</th>
    </tr>
    <%
        List<HitCourse> hitCourses = HitCourseDAO.getHitCourses(user.getId());
        request.setAttribute("hitCourses", hitCourses);
    %>
    <c:forEach var="hitCourse" items="${hitCourses}">
        <tr>
            <td>
                <label>
                    <input type="checkbox"
                           id="${hitCourse.cId}"
                           name="checkbox">
                </label>
            </td>
            <td>
                    ${hitCourse.cId}
            </td>
            <td>
                    ${hitCourse.cName}
            </td>
            <td>
                    ${hitCourse.score}
            </td>
        </tr>
    </c:forEach>
</table>
<button onclick="onBtClick()">退选</button>

<button onclick="window.location.href='./allCourse.jsp'">课程管理</button>
<script type="text/javascript">
    function onBtClick() {
        let tb = document.getElementById('table_hitCourse');    // table 的 id
        let rows = tb.rows;                           // 获取表格所有行
        let post_str = '';
        for (let i = 1; i < rows.length; i++) {
            let input = rows[i].cells[0].childNodes[1].childNodes[1];
            if (input.checked) {
                post_str = post_str.concat(input.id).concat("=1&");
            }
        }
        if (post_str.length === 0) {
            alert("没有选择课程！");
            return;
        }
        post_str = post_str.concat('exit=true');
        let httpRequest = new XMLHttpRequest();//第一步：建立所需的对象
        httpRequest.open('POST', '${pageContext.request.contextPath}/servlets/course/ExitCourse', true);//第二步：打开连接  将请求参数写在url中
        httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");//设置请求头 注：post方式必须设置请求头（在建立连接后设置请求头）
        httpRequest.send(post_str);//第三步：发送请求  将请求参数写在URL中
        /**
         * 获取数据后的处理程序
         */
        httpRequest.onreadystatechange = function () {
            if (httpRequest.readyState === 4 && httpRequest.status === 200) {
                let result = httpRequest.responseText;//获取字符串
                if (result === "<%=ExitCourse.SUCCESS_CODE%>") {
                    location.reload();
                }
                alert(result);
            }
        };
    }
</script>
<c:if test="${sessionScope.addCourseFailure != null}">
    <script type="text/javascript">
        alert("由于数据库连接异常或者您所选的课有重复，部分您所选的课程没有成功选择");
        <%
        /*
        重新置空
         */
        request.getSession().setAttribute("addCourseFailure",null);
        %>
    </script>
</c:if>
</body>
</html>
